package com.jzo2o.orders.manager.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzo2o.api.orders.dto.response.OrderResDTO;
import com.jzo2o.api.orders.dto.response.OrderSimpleResDTO;
import com.jzo2o.common.expcetions.CommonException;
import com.jzo2o.common.utils.ObjectUtils;
import com.jzo2o.orders.base.enums.OrderPayStatusEnum;
import com.jzo2o.orders.base.enums.OrderStatusEnum;
import com.jzo2o.orders.base.mapper.OrdersMapper;
import com.jzo2o.orders.base.model.domain.Orders;
import com.jzo2o.orders.manager.service.IOrdersManagerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 订单表 服务实现类
 * </p>
 *
 * @author itcast
 * @since 2023-07-10
 */
@Slf4j
@Service
public class OrdersManagerServiceImpl extends ServiceImpl<OrdersMapper, Orders> implements IOrdersManagerService {

    @Override
    public List<Orders> batchQuery(List<Long> ids) {
        LambdaQueryWrapper<Orders> queryWrapper = Wrappers.<Orders>lambdaQuery().in(Orders::getId, ids).ge(Orders::getUserId, 0);
        return baseMapper.selectList(queryWrapper);
    }

    @Override
    public Orders queryById(Long id) {
        return baseMapper.selectById(id);
    }

    /**
     * 滚动分页查询
     *
     * @param currentUserId 当前用户id
     * @param ordersStatus  订单状态，0：待支付，100：派单中，200：待服务，300：服务中，400：待评价，500：订单完成，600：已取消，700：已关闭
     * @param sortBy        排序字段
     * @return 订单列表
     */
    @Override
    public List<OrderSimpleResDTO> consumerQueryList(Long currentUserId, Integer ordersStatus, Long sortBy) {
        //1.构件查询条件
        LambdaQueryWrapper<Orders> queryWrapper = Wrappers.<Orders>lambdaQuery()
                .eq(ObjectUtils.isNotNull(ordersStatus), Orders::getOrdersStatus, ordersStatus)
                .eq(Orders::getUserId, currentUserId)
                .orderByDesc(Orders::getCreateTime);

        Page<Orders> queryPage = new Page<>();
        //queryPage.addOrder(OrderItem.desc(SORT_BY));
        queryPage.setSearchCount(false);

        //2.查询订单列表
        Page<Orders> ordersPage = baseMapper.selectPage(queryPage, queryWrapper);
        List<Orders> records = ordersPage.getRecords();
        List<OrderSimpleResDTO> orderSimpleResDTOS = BeanUtil.copyToList(records, OrderSimpleResDTO.class);
        return orderSimpleResDTOS;

    }

    /**
     * 根据订单id查询
     *
     * @param id 订单id
     * @return 订单详情
     */
    @Override
    public OrderResDTO getDetail(Long id) {
        Orders orders = queryById(id);
        OrderResDTO orderResDTO = BeanUtil.toBean(orders, OrderResDTO.class);
        return orderResDTO;
    }

    /**
     * 订单评价
     *
     * @param ordersId 订单id
     */
    @Override
    @Transactional
    public void evaluationOrder(Long ordersId) {
//        //查询订单详情
//        Orders orders = queryById(ordersId);
//
//        //构建订单快照
//        OrderSnapshotDTO orderSnapshotDTO = OrderSnapshotDTO.builder()
//                .evaluationTime(LocalDateTime.now())
//                .build();
//
//        //订单状态变更
//        orderStateMachine.changeStatus(orders.getUserId(), orders.getId().toString(), OrderStatusChangeEventEnum.EVALUATE, orderSnapshotDTO);
    }

    /**
     * 支付成功
     *
     * @param transactionInfo 支付信息
     */
    @Override
    public void paySuccess(String transactionInfo) {
        /**
         *    transactionInfo.put("orderNo", trading.getProductOrderNo());
         *         transactionInfo.put("tradingNo", transaction.getOutTradeNo());
         *         transactionInfo.put("payTime", transaction.getSuccessTime());
         *         transactionInfo.put("payChannel", PayChannelEnum.WECHAT_PAY.getValue());
         */
        log.info("支付成功,开始解析mq信息");
        //解析支付信息
        HashMap bean = JSONUtil.toBean(transactionInfo, HashMap.class);
        Long orderNo = (Long) bean.get("orderNo");
        String tradingNo = (String) bean.get("tradingNo");
        String payTime = (String) bean.get("payTime");
        String payChannel = (String) bean.get("payChannel");

        //修改订单状态
        if (orderNo == null || tradingNo == null) {
            log.error("订单号、交易单号");
            throw new CommonException("订单号、交易单号");
        }
        Orders orders = queryById(orderNo);
        if (orders == null) {
            log.error("订单不存在");
            throw new CommonException("订单不存在");
        }
        log.info("消息解析成功，开始修改订单信息");
        //修改订单状态
        orders.setOrdersStatus(OrderStatusEnum.DISPATCHING.getStatus());
        orders.setPayStatus(OrderPayStatusEnum.PAY_SUCCESS.getStatus());
        //TODO 时间 暂时不处理

/*        //完善订单信息
        orders.setTradingOrderNo(Long.valueOf(tradingNo));
        // 使用 OffsetDateTime 解析带时区的时间字符串
        OffsetDateTime offsetDateTime = OffsetDateTime.parse(payTime);
        // 转换为 LocalDateTime（去掉时区信息）
        LocalDateTime localDateTime = offsetDateTime.toLocalDateTime();
        orders.setPayTime(localDateTime);*/
        orders.setTradingChannel(payChannel);
        super.updateById(orders);
    }
}
